import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class BrowserOperation:  # 定义一个浏览器操作类
    def __init__(self):  # 初始化对象属性
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.so.com/')

    def quit(self, seconds=2):  # 关闭浏览器方法,默认2s后全部关闭
        time.sleep(seconds)
        self.driver.quit()

    def windows_size(self):
        self.driver.set_window_size(480, 800)  # 调整浏览器大小为固定尺寸
        time.sleep(2)
        self.driver.maximize_window()  # 浏览器最大化
        time.sleep(2)
        self.driver.minimize_window()  # 浏览器最小化

    def forward_back(self):
        self.driver.back()  # 浏览器后退
        time.sleep(2)
        self.driver.forward()  # 浏览器前进
        time.sleep(2)
        self.driver.refresh()  # 浏览器刷新
        time.sleep(2)

    def close(self):
        self.driver.find_element(By.LINK_TEXT, '资讯').click()  # 查找资讯元素点击打开网页
        time.sleep(2)
        self.driver.close()


if __name__ == '__main__':
    case = BrowserOperation()
    # case.windows_size()
    # case.forward_back()
    case.close()
